//
//  SearchView.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/20.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "SearchView.h"
#import "config.h"


@interface SearchView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIView *parentView;
@property (nonatomic,strong)UIButton *blackBtn;



-(void)btnOnClick:(UIButton*)sender;

@end

@implementation SearchView
@synthesize blackBtn,tableView,parentView,textField,delegate;


-(instancetype)initWithParentView:(UIView*)view withEt:(UITextField*)et{
    
    self = [super init];
    if (self) {
        
        parentView = view.superview;
        textField  = et;
//        textField.delegate = self;
        
        blackBtn  = [[UIButton alloc]initWithFrame:CGRectMake(view.frame.origin.x , view.frame.origin.y, PMW, view.frame.size.height)];
        [blackBtn setBackgroundColor:[UIColor blackColor]];
        [blackBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [blackBtn setAlpha:0.6];
        
        
        tableView = [[ScroDeleteTableView alloc]initWithFrame:blackBtn.frame];
        tableView.delegate = self;
        tableView.dataSource = self;
        
 
        if (isIOS7) {
            if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
                [self.tableView setSeparatorInset:UIEdgeInsetsZero];
            }
        }
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}


-(void)show{
    
    [parentView addSubview:blackBtn];
    [parentView addSubview:tableView];
//    [tableView reloadData];
}


-(void)hide{

    [blackBtn removeFromSuperview];
    [tableView removeFromSuperview];
    [self.textField resignFirstResponder];
     self.textField.text = @"";
}


-(void)btnOnClick:(UIButton*)sender{
    
    if (sender==blackBtn) {
        [self.delegate hideSearchView];
        [self.textView setText:@""];
        [self.textView resignFirstResponder];
        [self.textView textDicChange:@""];
        [self hide];
    }
}



-(void)tableViewReloadAndUpFrame{
    
    [self.tableView reloadData];

    int itemH = [self.delegate getCellHight];
    int count = [self.delegate getCellCount];
    int allH = itemH*count;
    
    CGRect buf = self.tableView.frame;
    buf.size.height = allH;
    [self.tableView setFrame:buf];
}


#pragma mark tableView delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [self.delegate getCellHight];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return [self.delegate getCellCount];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [self.delegate getCellByTableView:self.tableView WithIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"----点击");
}

#pragma mark textField delegate
//- (void)textFieldDidBeginEditing:(UITextField *)textField{
//
//    [self show];
//    
//    [self upTableViewFrame];
//    
//}
//
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    
//    
//    [self.tableView reloadData];
//    
//    [self upTableViewFrame];
//
//    return true;
//}

@end
